package com.df.barcodescaning.util;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

public class FileUtils {

	/** 
     * 通过Uri返回File文件
     *  content://com.android.externalstorage.documents/document/primary%3ADownload%2Fsaf.csv
     * 注意：通过相机的是类似content://media/external/images/media/97596 
     * 通过相册选择的：file:///storage/sdcard0/DCIM/Camera/IMG_20150423_161955.jpg 
     * 通过查询获取实际的地址 
     * @param uri 
     * @return 
     */  
	 public static String getRealFilePath( final Context context, final Uri uri ) {
		if ( null == uri ) return null;
		final String scheme = uri.getScheme();
		String data = null;
		if ( scheme == null )
			data = uri.getPath();
		else if ( ContentResolver.SCHEME_FILE.equals( scheme ) ) {
			data = uri.getPath();
		} else if ( ContentResolver.SCHEME_CONTENT.equals( scheme ) ) {
			Cursor cursor = context.getContentResolver().query( uri, new String[] { MediaStore.Images.ImageColumns.DATA }, null, null, null );
			if ( null != cursor ) {
				if ( cursor.moveToFirst() ) {
					int index = cursor.getColumnIndex( MediaStore.Images.ImageColumns.DATA );
					if ( index > -1 ) {
						data = cursor.getString( index );
					}
				}
				cursor.close();
			}
		}
		return data;
	}

	/**
	 * 检测文件是否存在,不存在则创建
	 * @param filePath
	 * @return
	 */
	public static boolean checkDir(String filePath){
		File file = new File(filePath);
		if(!file.exists()){
			return file.mkdirs();
		}
		return true;
	}

	/**
	 * 判断SD卡是否存在并具有读取权限
	 * @return
	 */
	public static boolean isExistSDCard(){
		if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
			return true;
		}
		return false;
	}

	/**
	 * Java文件操作 获取文件扩展名
	 * @param filename
	 * @return
	 */
	public static String getExtensionName(String filename){
		if ((filename!=null) && (filename.length()>0)) {
			int dot = filename.lastIndexOf(".");
			if (dot>-1 && dot<(filename.length()-1)) {
				return filename.substring(dot +1);
			}
		}
		return filename;
	}

	/**
	 *  获取SD卡路径
	 */
	public static String getSDCardPath() {
		if (isExistSDCard()) {
			return Environment.getExternalStorageDirectory().toString() + "/";
		}
		return null;
	}

	/**
	 * @Description: 获取当前应用SD卡缓存目录
	 * @param context
	 * @return
	 */
	public static String getSDCacheDir(Context context) {
		//api大于8的版本
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
			//目录为/mnt/sdcard/Android/data/com.mt.mtpp/cache
			return context.getExternalCacheDir().getPath();
		}
		String cacheDir = "/Android/data/" + context.getPackageName() + "/cache";
		return Environment.getExternalStorageDirectory().getPath() + cacheDir;

	}

	/**
	 * 获取缓存路径
	 * @param context
	 * @return
	 */
	public static String getCacheDir(Context context){
		String cacheDir;
		if (FileUtils.isExistSDCard()) {
			//SD存在
			cacheDir = FileUtils.getSDCacheDir(context);
			Log.e("----Main----", "--存在--缓存目录---->>>:" + cacheDir);
		}else{
			//不存在则使用系统目录
			cacheDir = context.getCacheDir().getPath();
			Log.e("----Main----", "----缓存目录---->>>:" + cacheDir);
		}
		cacheDir+="/";
//		L.e("----缓存目录---->>>:" + cacheDir);

//		checkDir(cacheDir);
		return cacheDir;
	}

	/**
	 * 读取指定文件夹下的文件
	 * @param filepath
	 * @return
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public static ArrayList<String> readfile(String filepath) throws FileNotFoundException, IOException {
		ArrayList list = new ArrayList<String>();
		try {
			File file = new File(filepath);
			if (!file.isDirectory()) {
				Log.e("----Main----", "文件");
				Log.e("----Main----", "path=" + file.getPath());
				Log.e("----Main----", "absolutepath=" + file.getAbsolutePath());
				Log.e("----Main----", "name=" + file.getName());

			} else if (file.isDirectory()) {
				Log.e("----Main----", "文件夹");
				String[] filelist = file.list();
				for (int i = 0; i < filelist.length; i++) {
					File readfile = new File(filepath + filelist[i]);
					if (!readfile.isDirectory()) {
						Log.e("----Main----", "path=" + readfile.getPath());
						Log.e("----Main----", "absolutepath="
								+ readfile.getAbsolutePath());
						Log.e("----Main----", "name=" + readfile.getName());
						list.add(readfile.getAbsolutePath());
					} else if (readfile.isDirectory()) {
						readfile(filepath + "\\" + filelist[i]);
					}
				}
			}
		} catch (FileNotFoundException e) {
			Log.e("----Main----", "readfile()   Exception:" + e.getMessage());
		}
		return list;
	}
}
